package com.hui.service.impl;

import com.hui.bean.RecordBean;
import com.hui.bean.StudentBean;
import com.hui.dao.impl.StudentDaoImpl;
import com.hui.service.IStudentService;

import java.sql.SQLException;
import java.util.List;

/**
 * 学生业务层实现类
 */
public class StudentServiceImpl implements IStudentService {

    private StudentDaoImpl studentDao;

    public StudentServiceImpl(){
        try {
            studentDao=StudentDaoImpl.class.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<StudentBean> findAllStudents() {
        try {
            List<StudentBean> allStudent = studentDao.getAllStudent();
            return allStudent;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<StudentBean> findStudentsByCondition(String studentName, String className, String roomAddress) {
        try {
            return studentDao.getStudentByCondition(studentName, className, roomAddress);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean addStudentInRoom(StudentBean studentBean) {
        int result=0;
            try {
                 result= studentDao.saveStudent(studentBean);

            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            }
        return result==1;
    }

    @Override
    public StudentBean showStudentInfoByStudentId(int studentId) {
        if (studentId!=-1){
            try {
                return studentDao.getStudentByStudentId(studentId);
            } catch (SQLException e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }


    @Override
    public boolean deleteStudentFromRoomByStudentId(int studentId) {
        try {
            return studentDao.deleteStudentByStudentId(studentId)==1;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean changeStudentInRoom(int studentId,int oldRoomId, int newRoomId) {
        try {
            return studentDao.updateStudentRoom(studentId,oldRoomId,newRoomId)==1;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public List<RecordBean> getRecordesByRoomId(int RoomId) {
        return null;
    }
}
